package com.social.instadownloader.util;

import android.content.ClipData;
import android.content.Context;
import android.widget.Toast;

public class ClipboardUtil {

	@SuppressWarnings("deprecation")
	public static void putText(Context context,String text){
	    int sdk = android.os.Build.VERSION.SDK_INT;
	    if(sdk < android.os.Build.VERSION_CODES. HONEYCOMB) {
	        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
	        clipboard.setText(text);
	    } else {
	        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); 
	        android.content.ClipData clip = ClipData.newPlainText("simple text",text);
	        clipboard.setPrimaryClip(clip);
	    }
	    Toast.makeText(context, text+"copied to clipboard", Toast.LENGTH_SHORT).show();
	}

	@SuppressWarnings("deprecation")
	public static String getText(Context context){
	    String text = null;
	    int sdk = android.os.Build.VERSION.SDK_INT;
	    if(sdk < android.os.Build.VERSION_CODES. HONEYCOMB ) {
	        try{
	        	android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
		        text =  clipboard.getText().toString();
	        }catch(Exception e){
	        	e.printStackTrace();
	        	return "";
	        }
	    } else {
	        try{
	        	android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); 
		        text =  clipboard.getText().toString();
	        }catch(Exception e){
	        	e.printStackTrace();
	        	return "";
	        }
	    }
	    return text;
	}
}
